package com.tomtop.example_redis.controller;

import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tomtop.core.Context;

@RestController
@RequestMapping("/session")
public class TestController {
	@PostMapping Object set(@RequestParam String name,@RequestParam String value) {
		HttpSession session = Context.getSession();
		session.setAttribute(name, value);
		return session.getAttribute(name);
	}
	@GetMapping Object get(@RequestParam String name) {
		HttpSession session = Context.getSession();
		return session.getAttribute(name);
	}
	@GetMapping("/list") Map<String,Object> list() {
		HttpSession session = Context.getSession();
		Enumeration<String> names = session.getAttributeNames();
		Map<String,Object> map=new LinkedHashMap<>();
		while (names.hasMoreElements()) {
			String name = (String) names.nextElement();
			map.put(name, session.getAttribute(name));
		}
		return map;
	}
}
